package cn.etgps.etgps.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;

import com.umeng.analytics.MobclickAgent;

import java.util.LinkedList;

import cn.etgps.etgps.dataEntity.Model;
import cn.etgps.etgps.dialog.DialogCreater;
import cn.etgps.etgps.utils.MainUsedInterface;

/**
 * 所有Activity的父类，提供一些公共函数
 */
public class BaseActivity extends FragmentActivity implements MainUsedInterface.MainButtonClickListener {
    /**
     * 记录打开过的activity，便于处理推出程序功能
     */
    public static LinkedList<Activity> mAllActivitys = new LinkedList<Activity>();
    /**
     * Manage dialog
     */
    private DialogCreater mDialogCreater = null;
    /**
     * 标记是否可以显示Dilaog
     */
    private boolean isCanShowDialog = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAllActivitys.add(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialogCreater = new DialogCreater(this, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isCanShowDialog = true;
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAllActivitys.remove(this);
    }

    /**
     * 统一的提供显示对话框的功能
     */
    public void showNormalDialog(final int dialogId) {
        if (!isCanShowDialog) {
            return;
        }
        try {
            this.runOnUiThread(new Runnable() {
                public void run() {
                    mDialogCreater.showDialog(getSupportFragmentManager(), dialogId);
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 统一提供的关闭对话框
     */
    public void dismissNormalDialog() {
        try {
            this.runOnUiThread(new Runnable() {
                public void run() {
                    mDialogCreater.dismissAllowingStateLoss();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        isCanShowDialog = false;
        super.onSaveInstanceState(outState);
    }

    @Override
    public void clickListListener(String listName, int index, Model model) {

    }

    @Override
    public void clickButtonListener(String name) {
        if (name.equals("Exit_app")) {
            for (Activity activity : mAllActivitys) {
                if (activity != null) {
                    activity.finish();
                }
            }
            mAllActivitys.clear();
            System.exit(0);
        } else if (name.equals("Logout")) {
            for (Activity activity : mAllActivitys) {
                if (activity != null && !activity.getClass().getName().equals(LoginActivity.class.getName())) {
                    activity.finish();
                }
            }
        }
    }

    @Override
    public void getInputStringListener(String btnName, String[] inputs) {

    }
}
